package com.cskaoyan.order.service.impl;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.order.constant.OrderRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderItemDto;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderConverter orderConverter;



    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        return null;
    }

    @Override
    public OrderDetailResponse orderDetail(String orderId) {
        //1.这里对应的是result的orderItem -> goodslist
        List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
        List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

        if (orderItems == null) {
            // 未找到对应的订单
            throw new BizException(OrderRetCode.CALL_PRODUCT_DETAIL_FAIL.getCode(),
                    OrderRetCode.CALL_PRODUCT_DETAIL_FAIL.getMessage());
        }

        //2.对应username，userid
        Order order = orderMapper.selectByPrimaryKey(orderId);

        //3.这里是对应result的receiverPhone和receiverAddress-->tel和streetName-->
        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);

        OrderDetailResponse orderDetailResponse= orderConverter.order2res(order, orderShipping);//2，3一起转过去


        //对应orderTotal
        Long orderTotal = orderMapper.countAll();

        //赋值
        orderDetailResponse.setOrderTotal(new BigDecimal(orderTotal));
        orderDetailResponse.setGoodsList(orderItemDtos);

        return orderDetailResponse;

    }
}
